#include "StdAfx.h"
#include "WEGlobal.h"


BOOL IsCharInStr(TCHAR ch, LPCTSTR pStr)
{
	while (*pStr)
	{
		if (ch == *pStr)
		{
			return TRUE;
		}
		pStr++;
	}
	return FALSE;
};

BOOL ConvertRectStrToRect(LPCTSTR pRectStr, CRect &rc)
{
	if (NULL == pRectStr)
	{
		return FALSE;
	}

	BOOL bResult = FALSE;

	LPCTSTR pDigit[4] = {0};
	LPCTSTR pSzNotValid = _T("\t\r\n ,-");

	const TCHAR *pInd = pRectStr;
	int nDigCount = 0;
	while (*pInd)
	{
		if ( _istdigit(*pInd) 
			&&(    (pInd == pRectStr) ||   IsCharInStr(*(pInd - 1), pSzNotValid) )
			)
		{
			if ((pInd != pRectStr) && ('-' == *(pInd - 1)))
			{
				pDigit[nDigCount] = pInd - 1;
			}else
			{
				pDigit[nDigCount] = pInd;
			}
			nDigCount++;
			if (4 == nDigCount)
			{
				break;
			}
		}
		pInd++;
	}

	if (4 != nDigCount)
	{
		return FALSE;
	}else
	{
		rc.left = _ttoi(pDigit[0]);
		rc.top = _ttoi(pDigit[1]);
		rc.right = rc.left + _ttoi(pDigit[2]);
		rc.bottom = rc.top + _ttoi(pDigit[3]);

		return TRUE;
	}
}

BOOL WEWinExec(LPCWSTR pExeFile)
{
	PROCESS_INFORMATION pi = {0};
	WCHAR cmdLine[256] = {0};
#ifdef _WIN32_WCE
	BOOL b = CreateProcess(pExeFile, cmdLine, NULL, NULL, FALSE, 0,NULL, NULL, NULL, &pi);
	if (b)
	{
		CloseHandle(pi.hThread);
		CloseHandle(pi.hProcess);
	}
	return b;
#else
	STARTUPINFO	si = {0};
	BOOL b = CreateProcess(pExeFile, cmdLine, NULL, NULL, FALSE, 0,NULL, NULL, &si, &pi);
	if (b)
	{
		CloseHandle(pi.hThread);
		CloseHandle(pi.hProcess);
	}
	return b;
#endif
}

void WEGetExePath(TCHAR* pExePath)
{
	ASSERT(NULL != pExePath);
	::GetModuleFileName(NULL, pExePath, MAX_PATH);
	int nExePathLen = (int)_tcslen(pExePath);
	for (int i = (nExePathLen-1); i>=0; i--)
	{
		if (pExePath[i] == _T('\\'))
		{
			pExePath[i + 1] = _T('\0');
			break;
		}
	}
}